import _lang from 'lodash/lang'
import Vue from 'vue'

export default {
    store: {
        get(key) {
            try {
                const data = JSON.parse(localStorage.getItem(key))
                return data
            } catch (e) {
                return localStorage.getItem(key)
            }
        },
        set(key, value) {
            if (_lang.isObject(value) || _lang.isArray(value)) {
                localStorage.setItem(key, JSON.stringify(value))
            } else {
                localStorage.setItem(key, value)
            }
        }
    },
    scrollToTop: () => {
        Vue.nextTick(() => {
            let Y = window.scrollY
            let h = window.document.body.scrollHeight
            const s = h / 20
            const wy = setInterval(() => {
                console.log(Y)
                Y -= s
                window.scrollTo(0, Y)
                if (Y <= 0) {
                    clearInterval(wy)
                }
            }, 20)
        })
    },
    getUrlParam(name) {
        // 获取参数
        var url = window.location.search;
        // 正则筛选地址栏
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        // 匹配目标参数
        var result = url.substr(1).match(reg);
        //返回参数值
        return result ? decodeURIComponent(result[2]) : null;
    }
}
